package com.java.travel.controller;

import com.java.travel.entity.Result;
import com.java.travel.entity.User;
import com.java.travel.service.UserService;
import com.java.travel.utils.CreateImageCode;
import com.java.travel.utils.ValidateImageCodeUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * description:
 * author:张腾
 * date:2021-03-07
 */
@RestController
@RequestMapping("user")
@CrossOrigin
@Api(tags = "用户模块")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @param user
     * @param request
     * @return
     */
    @RequestMapping("login")
    public Result login(@RequestBody User user,HttpServletRequest request){
        Result result = new Result();
        try {
            User userDB = (User) userService.login(user);
            //尚未使用redis，使用HttpServletRequest不佳
            request.getServletContext().setAttribute(userDB.getId(),userDB);
            result.setMsg("登陆成功！").setUserId(userDB.getId());
        }catch (Exception e){
            result.setState(false).setMsg(e.getMessage());
        }
        return result;
    }

    /**
     * 用户注册
     * @param code
     * @param key
     * @param user
     * @param request
     * @return
     */
    @RequestMapping("register")
    public Result register(String code,String key, @RequestBody User user, HttpServletRequest request){
        Result result = new Result();
        //验证验证码
        String keyCode = (String) request.getServletContext().getAttribute(key);
        try {
            if (keyCode.equalsIgnoreCase(code)){
                //注册用户
                userService.register(user);
                result.setMsg("注册成功！！");
            }else{
                throw new RuntimeException("验证码错误!!!");
            }
        } catch (Exception e){
            e.printStackTrace();
            result.setMsg(e.getMessage()).setState(false);
        }
        return result;
    }

    /**
     * 获取验证码
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("getImage")
    public Map<String, String> getImage( HttpServletRequest request) throws IOException {
        Map<String,String> map = new HashMap<>();
        CreateImageCode createImageCode = new CreateImageCode();
        //获取验证码
        String securityCode = createImageCode.getCode();
        //验证码存入seesion
        String key = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        request.getServletContext().setAttribute(key,securityCode);
        //生成图片
        BufferedImage image = createImageCode.getBuffImg();
        //进行base64编码
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ImageIO.write(image,"png",bos);
        String string = Base64Utils.encodeToString(bos.toByteArray());
        map.put("key",key);
        map.put("image",string);
        return map;
    }
}
